package com.lizhi.yun.core.heart.handler;

import com.lizhi.common.yun.entity.RpcActiveMqPOJO;
import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.common.yun.enums.UppSystemEnum;
import com.lizhi.utils.PropertiesUtil;
import com.lizhi.yun.base.common.Constants;
import com.lizhi.yun.core.eureka.handler.EurekaInfoHandler;
import com.lizhi.common.yun.entity.InstancePOJO;
import com.lizhi.yun.db.dao.UppOrganDao;
import com.lizhi.yun.db.dao.UppParametersDao;
import com.lizhi.yun.db.dao.UppSystemDao;
import com.lizhi.yun.db.pojo.UppOrganPOJO;
import com.lizhi.yun.db.pojo.UppParametersPOJO;
import com.lizhi.yun.db.pojo.UppSystemPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/6 15:22
 */
@Slf4j
@Service
public class UppHeartHandler {

    @Value(Constants.GATEWAY_ENABLE)
    private boolean enable;

    @Resource
    private UppSystemDao uppSystemDao;

    @Resource
    private UppOrganDao uppOrganDao;

    @Resource
    private UppParametersDao uppParametersDao;

    @Resource
    private EurekaInfoHandler eurekaInfoHandler;

    /**
     * 接收心跳数据
     * @param json
     * @return
     */
    public String heartBeat(String json){
        RpcClientPOJO rpcClientPOJO = SamJsonUtil.toObject(json, RpcClientPOJO.class);
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        UppSystemPOJO uppSystemPOJO = new UppSystemPOJO();
        uppSystemPOJO.setSystemType(rpcClientPOJO.getSystemType());
        uppSystemPOJO.setOrganId(rpcClientPOJO.getOrganId());
        uppSystemPOJO.setOrganNm(rpcClientPOJO.getOrganNm());
        uppSystemPOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        uppSystemPOJO.setBkOrganNm(rpcClientPOJO.getBkOrganNm());
        uppSystemPOJO.setHeartTm(DateUtils.getNowDate());
        uppSystemPOJO.setSystemStatus(UppSystemEnum.SYSTEM_STATUS_00.getEnValue());
        uppSystemPOJO.setIstTm(DateUtils.getNowDate());

        /*判断该医院信息是否查询到*/
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(rpcClientPOJO.getOrganId());
        if (StringUtils.isEmpty(uppOrganPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        } else {
            uppSystemPOJO.setOrganNm(uppOrganPOJO.getOrganNm());
        }


        /*根据系统类别查询配置信息*/
        UppParametersPOJO uppParametersPOJO = uppParametersDao.selectById(rpcClientPOJO.getSystemType());
        if (!StringUtils.isEmpty(uppParametersPOJO)){
            uppSystemPOJO.setBkOrganNm(uppParametersPOJO.getVal());
        }

        UppSystemPOJO systemPOJO = uppSystemDao.selectByOrganId(rpcClientPOJO.getSystemType(),rpcClientPOJO.getOrganId(),rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(systemPOJO)){
            uppSystemPOJO.setSystemId(StringUtils.newId());
            uppSystemDao.insertRecord(uppSystemPOJO);
        } else {
            uppSystemPOJO.setSystemId(systemPOJO.getSystemId());
            uppSystemDao.updateByIdSelective(uppSystemPOJO);
        }

        /*判断系统是否在线*/
        List<InstancePOJO> instancePOJOS = eurekaInfoHandler.getOtherDeploys(rpcServicePOJO.getSystemType());
        if (enable){
            if (instancePOJOS.size() > 0){
                rpcServicePOJO.setInstancePOJOS(eurekaInfoHandler.getGatewayDeploys(rpcServicePOJO.getSystemType()));
                rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
                rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
            } else {
                rpcServicePOJO.setInstancePOJOS(null);
                rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
                rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
            }
        } else {
            rpcServicePOJO.setInstancePOJOS(instancePOJOS);
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        }

        return SamJsonUtil.toJson(rpcServicePOJO);
    }

    /**
     * 接收心跳数据
     * @param json
     * @return
     */
    public String newHeartBeat(String json){
        RpcClientPOJO rpcClientPOJO = SamJsonUtil.toObject(json, RpcClientPOJO.class);
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        UppSystemPOJO uppSystemPOJO = new UppSystemPOJO();
        uppSystemPOJO.setSystemType(rpcClientPOJO.getSystemType());
        uppSystemPOJO.setOrganId(rpcClientPOJO.getOrganId());
        uppSystemPOJO.setOrganNm(rpcClientPOJO.getOrganNm());
        uppSystemPOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        uppSystemPOJO.setBkOrganNm(rpcClientPOJO.getBkOrganNm());
        uppSystemPOJO.setHeartTm(DateUtils.getNowDate());
        uppSystemPOJO.setSystemStatus(UppSystemEnum.SYSTEM_STATUS_00.getEnValue());
        uppSystemPOJO.setIstTm(DateUtils.getNowDate());

        /*判断该医院信息是否查询到*/
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(rpcClientPOJO.getOrganId());
        if (StringUtils.isEmpty(uppOrganPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        } else {
            uppSystemPOJO.setOrganNm(uppOrganPOJO.getOrganNm());
        }


        /*根据系统类别查询配置信息*/
        UppParametersPOJO uppParametersPOJO = uppParametersDao.selectById(rpcClientPOJO.getSystemType());
        if (!StringUtils.isEmpty(uppParametersPOJO)){
            uppSystemPOJO.setBkOrganNm(uppParametersPOJO.getVal());
        }

        UppSystemPOJO systemPOJO = uppSystemDao.selectByOrganId(rpcClientPOJO.getSystemType(),rpcClientPOJO.getOrganId(),rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(systemPOJO)){
            uppSystemPOJO.setSystemId(StringUtils.newId());
            uppSystemDao.insertRecord(uppSystemPOJO);
        } else {
            uppSystemPOJO.setSystemId(systemPOJO.getSystemId());
            uppSystemDao.updateByIdSelective(uppSystemPOJO);
        }

        /*判断系统是否在线*/
        List<InstancePOJO> instancePOJOS = eurekaInfoHandler.getOtherDeploys(rpcServicePOJO.getSystemType());
        if (!StringUtils.isEmpty(instancePOJOS)){
            rpcServicePOJO.setInstancePOJOS(instancePOJOS);
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        } else {
            rpcServicePOJO.setInstancePOJOS(null);
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        }

        return SamJsonUtil.toJson(rpcServicePOJO);
    }

    /**
     *功能描述 获取activeMQ服务器的连接信息
     * @author lizhi
     * @date 2021/12/30
     * @param organId:
     * @return java.lang.String
     */
    public String activeMQConfig(String organId){
        RpcActiveMqPOJO rpcActiveMqPOJO = new RpcActiveMqPOJO();
        /*判断该医院信息是否查询到*/
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(organId);
        if (StringUtils.isEmpty(uppOrganPOJO)){
            return null;
        }
        rpcActiveMqPOJO.setUserName(PropertiesUtil.getProperties("activemq.username", "system"));
        rpcActiveMqPOJO.setPassword(PropertiesUtil.getProperties("activemq.password", "system"));
        rpcActiveMqPOJO.setBrokerUrl(PropertiesUtil.getProperties("activemq.brokerurl", "system"));
        return SamJsonUtil.toJson(rpcActiveMqPOJO);
    }
}
